package com.gjy.leetcode.ctci;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * 面试题 01.01. 判定字符是否唯一
 *
 * @author 宫静雨
 * @version 1.0
 * @since 2023-01-04 10:30:48
 */
public class A0101 {

    @Test
    public void test1() {
        String s = "leetcode";
        assertFalse(isUnique(s));
    }

    @Test
    public void test2() {
        String s = "abc";
        assertTrue(isUnique(s));
    }

    @Test
    public void test3() {
        System.out.println(isUnique(""));
    }

    public boolean isUnique3(String astr) {
        byte mark = 0;
        for (char c : astr.toCharArray()) {
            byte move_bit = (byte) (c - 'a');
            if ((mark & (1 << move_bit)) != 0) {
                return false;
            } else {
                mark |= (1 << move_bit);
            }
        }
        return true;
    }

    public boolean isUnique2(String astr) {
        if (astr.length() > 26) {
            return false;
        }
        byte[] map = new byte[26];
        for (char c : astr.toCharArray()) {
            if (map[c - 'a'] > 0) {
                return false;
            }
            map[c - 'a']++;
        }
        return true;
    }

    public boolean isUnique(String astr) {
        if (astr.length() == 0) {
            return true;
        }
        return Arrays.stream(astr.split(""))
                .collect(Collectors.toSet()).size() == astr.length();
    }
}
